<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\util\Callout;
use app\util\File;
use app\admin\model\StoreRoom;
use app\admin\model\StoreRoomBed;
use app\admin\model\Yuyue;

class SceneController extends CommonController {
    /*
     * 店铺房间列表
     * author xfwed 
     * date 2020.07.08
     */

    public function roomList() {
        $store_id = input('param.store_id');
        $robot_no = input('param.robot_no');
        $active = input('param.active');
        $smart_status = input('param.smart_status');
        $manage_account = input('param.manage_account');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;


        if ($store_id) {//审核状态 0未审核 1审核通过 2审核不通过	
            $where['r.store_id'] = $store_id;
        }
        if ($robot_no) {
            $where['r.robot_no'] = $robot_no;
        }

        if ($active) {
            $where['r.active'] = $active; //1.启用 2.未启用
        }

        if ($smart_status) {
            $where['r.smart_status'] = $smart_status;
        }
        if ($manage_account) {
            $where['r.manage_account'] = $manage_account;
        }


        $where = isset($where) ? $where : [];
        $where['r.is_delete'] = 1;
        $storeList = StoreRoom::alias('r')
                ->join('store s', 's.id = r.store_id', 'left')
                ->where($where)
                ->order('id desc')
                ->field('s.store_name,r.*')
                ->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $storeList);
    }

    /*
     * 添加店铺房间
     * author xfwed 
     * date 2020.07.08
     */

    public function addStoreRoom() {
        $rule = [
            'room_name' => 'require',
            'store_id' => 'require',
//            'robot_no' => 'require',
            'room_image' => 'require',
            'smart_status' => 'require',
        ];
        $msg = [
            'room_name.require' => '房间名称必须',
            'store_id.require' => '店铺id必须',
//            'robot_no.require' => '机器编码必须',
            'room_image.require' => '房间图片必须',
            'smart_status.require' => '是否为智能房间必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $robot_no = input('param.robot_no');
        if ($robot_no) {
            $storeRoomRobot = StoreRoom::where('robot_no', $robot_no)->where('is_delete', 1)->find();
            if ($storeRoomRobot) {
                return $this->ajaxReturn(3, '机器码已经占用，请核对', null);
            }
        }

        $storeRoomname = StoreRoom::where('room_name', $all['room_name'])->where('store_id', $all['store_id'])->where('is_delete', 1)->find();
        if ($storeRoomname) {
            return $this->ajaxReturn(3, '房间名称不能重复', null);
        }

        $data['room_name'] = $all['room_name'];
        $data['store_id'] = $all['store_id'];
        $data['robot_no'] = input('param.robot_no');
        $data['room_image'] = $all['room_image'];
        $data['smart_status'] = $all['smart_status'];
        $data['manage_account'] = input('param.manage_account');
        $data['scene_in_name'] = input('param.scene_in_name');//
        $data['scene_out_name'] = input('param.scene_out_name');
        $data['add_time'] = date('Y-m-d H:i:s');
        $row = StoreRoom::insert($data);
        if ($row) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 删除/开启/关闭店铺房间
     * author xfwed 
     * date 2020.07.08
     */

    public function delStoreRoom() {
        $id = input('param.id');
        $type = input('param.type'); //1删除 2开启 3关闭
        if ($type == 1) {
            $row = Yuyue::where('status = 3 and room_id =' . $id)
                    ->whereOr('status=4 and if_serviceing=1 and room_id =' . $id)
                    ->find();
            if ($row) {
                return $this->ajaxReturn(3, '房间占用中', null);
            }
            StoreRoom::where('id', $id)->update(['is_delete' => 2]);
            //删除房间所有的床位
            StoreRoomBed::where('room_id', $id)->update(['is_delete' => 2]);
            $note = '删除成功';
        } elseif ($type == 2) {
            StoreRoom::where('id', $id)->update(['active' => 1]);
            $note = '开启成功';
        } elseif ($type == 3) {
            StoreRoom::where('id', $id)->update(['active' => 2]);
            $note = '关闭成功';
        }

        return $this->ajaxReturn(0, $note, null);
    }

    /*
     * 店铺房间床位列表
     * author xfwed 
     * date 2020.07.08
     */

    public function roomBedList() {
        $room_id = input('param.room_id');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        $storeList = StoreRoomBed::where('room_id', $room_id)->where('is_delete', 1)->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $storeList);
    }

    /*
     * 添加房间床位
     * author xfwed 
     * date 2020.07.08
     */

    public function addStoreRoomBed() {
        $rule = [
            'room_id' => 'require',
            'bed_name' => 'require',
            'bed_image' => 'require',
        ];
        $msg = [
            'room_id.require' => '房间id必须',
            'bed_name.require' => '床位名称必须',
            'bed_image.require' => '床位图片必须',
        ];
        $all = input('param.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }
        $storeRoomBedName = StoreRoomBed::where('room_id', $all['room_id'])
                ->where('bed_name', $all['bed_name'])
                ->where('is_delete', 1)
                ->find();
        if ($storeRoomBedName) {
            return $this->ajaxReturn(3, '床位名称不能重复', null);
        }

        if (input('param.sleeptape_no')) {
            $storeRoomBed = StoreRoomBed::where('sleeptape_no', input('param.sleeptape_no'))
                    ->where('is_delete', 1)
                    ->find();
            if ($storeRoomBed) {
                return $this->ajaxReturn(3, '床位MAC不能重复', null);
            }
        }

        $data['room_id'] = $all['room_id'];
        $data['bed_name'] = $all['bed_name'];
        $data['bed_image'] = $all['bed_image'];
        $data['add_time'] = date('Y-m-d H:i:s');
        $data['sleeptape_no'] = input('param.sleeptape_no');
        $row = StoreRoomBed::insert($data);
        if ($row) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 删除/开启/关闭房间床位
     * author xfwed 
     * date 2020.07.08
     */

    public function delStoreRoomBed() {
        $id = input('param.id');
        $type = input('param.type'); //1删除 2开启 3关闭
        if ($type == 1) {
            $row = Yuyue::where('status = 3 and bed_id =' . $id)
                    ->whereOr('status=4 and if_serviceing=1 and bed_id =' . $id)
                    ->find();
            if ($row) {
                return $this->ajaxReturn(3, '床位占用中', null);
            }
            StoreRoomBed::where('id', $id)->update(['is_delete' => 2]);
            $note = '删除成功';
        } elseif ($type == 2) {
            StoreRoomBed::where('id', $id)->update(['active' => 1]);
            $note = '开启成功';
        } elseif ($type == 3) {
            StoreRoomBed::where('id', $id)->update(['active' => 2]);
            $note = '关闭成功';
        }

        return $this->ajaxReturn(0, $note, null);
    }

}
